let logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

let Remote = function (app) {
    this.app = app;
};

Remote.prototype.loginGame = function (msg, callback) {
    let frontendId = msg.frontendId;
    let sessionId = msg.sessionId;
    let gid = msg.gid;
    let minStack = msg.minStack;
    let maxStack = msg.maxStack || Number.MAX_VALUE;
    let playerService = this.app.get("playerService");
    let gameService = this.app.get("gameService");
    let game = gameService.getGameById(gid);
    if (!game) {
        logger.error("createYuanBaoTable invalid gid=" + gid);
        callback({
            resultCode: 404,
            message: "游戏id错误"
        });
        return;
    }
    let player = playerService.findByConnectionId(frontendId, sessionId);
    if (!player) {
        callback({
            resultCode: 404,
            message: "玩家未登陆"
        });
        return;
    }
    let currency = player.getDiamond();
    if (game.currencyType === 'gold') {
        currency = player.getGold();
    } else if (game.currencyType === 'fangka') {
        currency = player.getFangka();
    }
    if (gid === 10000) {
        if (currency < minStack) {
            callback({
                resultCode: 404,
                message: "游戏币不足"
            });
            return;
        }
        if (currency > maxStack) {
            callback({
                resultCode: 404,
                message: "您的游戏币很充足，去更高级的场玩吧！"
            });
            return;
        }

    } else {
        if (currency < minStack * 2) {
            callback({
                resultCode: 404,
                message: "游戏币不足"
            });
            return;
        }
    }

    if (gid === 10003 && player.getTotalRecharge() < 100) {
        callback({
            resultCode: 404,
            message: "该房间不存在"
        });
        return;
    }
    if (player.getPlayingGameId() !== 0) {
        logger.error("loginGame frontendId=" + frontendId + " sessionId=" + sessionId +
            " uid=" + player.getUid() + " gameId=" + player.getPlayingGameId() +
            " newGameId ="  + gid);
    }
    player.join(game);
    playerService.save(player);
    let playerJSON = player.toJSON();
    let account = playerJSON["account"];
    let result = {
        resultCode: 0,
        message: "ok",
        account: account,
        balance: currency
    };
    if (msg.action === "create") {
        result["code"] = gameService.genCode(game);
    }
    callback(result);
};

Remote.prototype.logoutGame = function (msg, callback) {
    let gid = msg.gid;
    let frontendId = msg.frontendId;
    let sessionId = msg.sessionId;
    let playerService = this.app.get("playerService");
    let gameService = this.app.get("gameService");
    let game = gameService.getGameById(gid);
    if (!game) {
        logger.error("logoutGame invalid gid=" + gid);
        callback({
            resultCode: 404,
            message: "游戏id错误"
        });
        return;
    }
    let player = playerService.findByConnectionId(frontendId, sessionId);
    if (!!player && player.getPlayingGameId() === gid) {
        player.leave();
        playerService.save(player);
    }
    callback({
        resultCode: 0,
        message: "ok"
    });
};

Remote.prototype.clearCode = function (msg, callback) {
    logger.info("clearCode. code=" + JSON.stringify(msg.codes));
    let codes = msg.codes;
    let gameService = this.app.get("gameService");
    gameService.clearCode(codes);
    callback({
        resultCode: 0,
        message: "ok"
    });
};

Remote.prototype.changeDiamond = function (msg, callback) {
    let uids = msg.uids;
    let amounts = msg.amounts;
    let params = msg.params;
    let length = uids.length;
    let playerService = this.app.get("playerService");
    for (let i = 0; i < length; i++) {
        let uid = uids[i];
        let amount = amounts[i];
        playerService.changeDiamond(uid, amount, params[i]);
    }
    callback({
        resultCode: 0,
        message: "ok"
    });
};

Remote.prototype.changeGold = function (msg, callback) {
    let uids = msg.uids;
    let amounts = msg.amounts;
    let params = msg.params;
    let length = uids.length;
    let playerService = this.app.get("playerService");
    for (let i = 0; i < length; i++) {
        let uid = uids[i];
        let amount = amounts[i];
        playerService.changeGold(uid, amount, params[i]);
    }
    callback({
        resultCode: 0,
        message: "ok"
    });
};
